﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;

namespace DAO
{
    public static class GiaoDichNapTienDAO
    {
        public static GiaoDichNapTienDTO LayTinBanTuDataRow(DataRow dr)
        {
            GiaoDichNapTienDTO tb = null;
            try
            {
                tb = new GiaoDichNapTienDTO();
                tb.MaGiaoDich = dr["MaGiaoDich"].ToString();
                tb.MaKhachHang = dr["MaKhachHang"].ToString();
                tb.ThoiGianNap = DateTime.Parse(dr["ThoiGianNap"].ToString());
                tb.SoTienNap = int.Parse(dr["SoTienNap"].ToString());
                tb.HinhThucNap = dr["HinhThucNap"].ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy thông tin tin bán: " + ex.ToString());
            }
            return tb;
        }

        public static IList<SqlParameter> TruyenThamSoChoStored(GiaoDichNapTienDTO item)
        {
            IList<SqlParameter> arrParameter = new List<SqlParameter>();
            arrParameter.Add(new SqlParameter("@MaGiaoDich", item.MaGiaoDich));
            arrParameter.Add(new SqlParameter("@MaKhachHang", item.MaKhachHang));
            arrParameter.Add(new SqlParameter("@ThoiGianNap", item.ThoiGianNap));
            arrParameter.Add(new SqlParameter("@SoTienNap", item.SoTienNap));
            arrParameter.Add(new SqlParameter("@HinhThucNap", item.HinhThucNap));
            return arrParameter;
        }

        public static void ThemGiaoDich(GiaoDichNapTienDTO gd)
        {
            try
            {
                IList<SqlParameter> arrParameter = TruyenThamSoChoStored(gd);
                DataProvider.ExecuteStoreProc("addGiaoDichNapTien", arrParameter);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thêm giao dich nap tien " + ex.ToString());
            }
        }

        public static GiaoDichNapTienDTO getGiaoDich(string makhachhang)
        {
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaNguoiGiaoDich", makhachhang));
                DataTable dt = DataProvider.ExecuteStoreProc("getGiaoDichNapTien", arrParameter);
                return LayTinBanTuDataRow(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lay giao dich nap tien " + ex.ToString());
            }
            return null;
        }
    }
}
